<?php

namespace Xiaoc\Model;

use PhalApi\Model\NotORMModel as NotORM;

class Goods extends NotORM
{
    /**
     * 猜你喜欢
     * @param int $user_id 用户ID
     * @param int $limit 查询的条数
     * @return array
     */
    function get_guess_goods($user_id, $limit = 4)
    {
        $prefix = \PhalApi\DI()->config->get('dbs.tables.__default__.prefix');

        $order_idArr = $finished_goods = $link_cats = array();

        $sql = 'SELECT order_id FROM ' . $prefix . 'order_info  WHERE user_id = \'' . $user_id . '\' ORDER BY order_id DESC LIMIT 5';
        $order_arr = \PhalApi\DI()->notorm->order_info->queryAll($sql, array());

        if ($order_arr) {
            foreach ($order_arr as $key => $val) {
                $order_idArr[] = $val['order_id'];
            }

            $order_str = db_create_in($order_idArr, 'og.order_id');
            $sql = 'SELECT g.goods_id, g.cat_id FROM ' . $prefix . 'order_goods' . ' AS og ' . 'LEFT JOIN ' . $prefix . 'goods AS g ON g.goods_id = og.goods_id ' . 'WHERE ' . $order_str . ' GROUP BY g.goods_id DESC';
            $cat_All = \PhalApi\DI()->notorm->order_goods->queryAll($sql, array());

            foreach ($cat_All as $kk => $vv) {
                $finished_goodsStr .= '\'' . $vv['goods_id'] . '\',';
                $link_cats[] = $vv['cat_id'];
            }

            $finished_goodsStr = substr($finished_goodsStr, 0, -1);
            $link_cats = array_unique($link_cats);
            $link_cats_str = db_create_in($link_cats, 'cat_id');
            $sql = 'SELECT goods_id, goods_name, goods_thumb, shop_price,sales_volume FROM ' . $prefix . 'goods WHERE ' . $link_cats_str . ' AND goods_id NOT IN (' . $finished_goodsStr . ') AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 ORDER BY sales_volume DESC  LIMIT 8';
            $query = \PhalApi\DI()->notorm->goods->queryAll($sql, array());
        }

        if ((empty($guess_goods) || (count($guess_goods) < $limit)) && ($history == 1)) {
            $guess_goods = array();
            $sql = 'SELECT goods_id, goods_name, goods_thumb, shop_price, user_id, sales_volume,market_price FROM ' . $prefix . 'goods WHERE  (sales_volume > 0 OR is_hot = 1) AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 ORDER BY sales_volume DESC LIMIT ' . $limit;
            $query = \PhalApi\DI()->notorm->goods->queryAll($sql, array());
        }

        foreach ($query as $row) {
            $guess_goods[$row['goods_id']]['goods_id'] = $row['goods_id'];
            $guess_goods[$row['goods_id']]['goods_name'] = $row['goods_name'];
            $guess_goods[$row['goods_id']]['sales_volume'] = $row['sales_volume'];
            $guess_goods[$row['goods_id']]['short_name'] = 0 < $GLOBALS['_CFG']['goods_name_length'] ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
            $guess_goods[$row['goods_id']]['goods_thumb'] = $row['goods_thumb'];
        }

        return $guess_goods;
    }

    /**
     * 首页海外特惠
     * @return mixed
     */
    public function importGoods()
    {
        $sql = "SELECT g.goods_id,g.cat_id,g.shop_price,g.goods_thumb,e.origincountry FROM dsc_goods AS g LEFT JOIN  dsc_goods_extend AS e ON g.goods_id = e.goods_id WHERE g.cat_id = 4 AND g.is_on_sale = 1 ORDER BY click_count DESC LIMIT 5 OFFSET 0";
        $rows = $this->getORM()->queryAll($sql, array());
        return $rows;
    }

    /**
     *  海外特惠列表
     * @param $page int 页数
     * @return mixed
     */
    public function importList($page)
    {
        $limit = ($page - 1) * 8;
        $sql = "SELECT g.goods_id,g.cat_id,g.shop_price,g.goods_thumb,e.origincountry FROM dsc_goods AS g LEFT JOIN  dsc_goods_extend AS e ON g.goods_id = e.goods_id WHERE cat_id = 4 ORDER BY click_count DESC LIMIT $limit OFFSET 8";
        $rows = $this->getORM()->queryAll($sql, array());
        return $rows;
    }

//    public function goodsListByCate($cate_name, $page, $limit)
//    {
//        $limit = ($page - 1) * $limit;
//        $cate_id = $this->getORM()->select('cat_id')->where(array('cat_name' => $cate_name));
//        $sql = 'SELECT g.goods_id,g.cat_id,g.shop_price,g.goods_thumb,e.origincountry FROM dsc_goods AS g LEFT JOIN  dsc_goods_extend AS e ON g.goods_id = e.goods_id WHERE cat_id = ' . $cate_id . ' ORDER BY click_count DESC LIMIT ' . $limit . ' OFFSET 8';
//        $rows = $this->getORM()->queryAll($sql, array());
//        return $rows;
//    }

//    public function goodsListByType($type, $page, $limit)
//    {
//        $limit = ($page - 1) * $limit;
//        $sql = 'SELECT goods_id,cat_id,shop_price,goods_thumb FROM dsc_goods  WHERE cat_id = ' . $cate_id . ' ORDER BY click_count DESC LIMIT ' . $limit . ' OFFSET 8'
//    }

//    public function moduleAd()
//    {
//
//    }
}
